<template>
  <div class="recommend" ref="recommend">
    <scroll ref="scroll" class="recommend-content" :data="discList">
      <div>
        <div v-if="recommends.length" class="slider-wrapper">
          <!-- <slider>
              <div v-for="item in recommends" :key="item.id">
                <a :href="item.linkUrl">
                  <img class="needsclick" @load="loadImage" :src="item.picUrl">
                </a>
              </div>
            </slider> -->
          <swiper :options="swiperOption">
            <swiper-slide v-for="item in recommends" :key="item.id">
              <a :href="item.linkUrl">
                <img :src="item.picUrl" alt="" class="item-img">
              </a>
            </swiper-slide>
            <div class="swiper-pagination" slot="pagination"></div>
          </swiper>
        </div>
        <div class="recommend-list">
          <h1 class="list-title">热门歌单推荐</h1>
          <ul>
            <li class="item" v-for="(item,index) in discList" :key="index" @click="selectItem(item)">
                <div class="icon">
                  <img v-lazy="item.imgurl" width="60" height="60" alt="">
                </div>
                <div class="text">
                  <h2 class="name" v-html="item.creator.name"></h2>
                  <div class="desc" v-html="item.dissname"></div>
                </div>
            </li>
          </ul>
        </div>
      </div>
      <div class="loading-container" v-show="!discList.length">
        <loading></loading>
      </div>
    </scroll>
    <router-view></router-view>
  </div>
</template>

<script type="text/ecmascript-6">
// import Slider from "base/slider/slider";
import slideSwiper from "base/slider/VueAwesomeSwiper";
import { getRecommend, getDiscList } from "api/recommend";
import { ERR_OK } from "api/config";
import axios from "axios"; // 引入axios 本处特殊因需要配置request headers所以局部注册
import Scroll from "base/scroll/scroll"; // 引入滚动组件
import Loading from "base/loading/loading"; // 引入loading组件
import {playlistMixin} from "common/js/mixin"; // 引入定义的 mixin组件，查看vue官网的mixins(目的时解决迷你底部播放器覆盖列表底部的问题)

import{mapMutations} from 'vuex'

export default {
  name: "recommend",
  mixins: [playlistMixin], // 可以查阅vue官网的mixins的用法
  data() {
    return {
      recommends: [], // 存放轮播图数组
      discList: [], // 热门歌单
      swiperOption: {
        autoplay: true,
        loop: true,
        pagination: {
          el: ".swiper-pagination"
        }
      }
    };
  },
  components: {
    // Slider,
    slideSwiper,
    Scroll,
    Loading
  },
  created() {
    this._getRecommend();
    this._getDiscList();
  },
  methods: {
    ...mapMutations({
      setDisc: 'SET_DISC'
    }),
    // vue中 mixins的使用(为了解决迷你播放器在底部挡住列表的最底部问题，引入vue中的mixins使用)
    handlePlaylist (playlist) {
      const bottom = playlist.length > 0 ? '60px': '';
      this.$refs.recommend.style.bottom = bottom;
      this.$refs.scroll.refresh();
    },
    // 跳转到歌单详情页
    selectItem (item) {
      this.$router.push({
        path: `/recommend/${item.dissid}`
      });
      this.setDisc(item)
    },
    _getRecommend() {
      getRecommend().then(res => {
        // 成功时返回
        if (res.code === ERR_OK) {
          this.recommends = res.data.slider;
        }
      });
    },
    _getDiscList() {
      getDiscList().then(res => {
        if (res.code === ERR_OK) {
          this.discList = res.data.list;
          console.log(this.discList)
        }
      });
    },
    loadImage() {
      if (!this.checkloaded) {
        this.checkloaded = true;
        this.$refs.scroll.refresh();
      }
    }
  }
};
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
@import '~common/stylus/variable';

.recommend {
  position: fixed;
  width: 100%;
  top: 88px;
  bottom: 0;

  .recommend-content {
    height: 100%;
    overflow: hidden;

    .slider-wrapper {
      position: relative;
      width: 100%;
      overflow: hidden;

      .item-img {
        width: 100%;
      }
    }

    .recommend-list {
      .list-title {
        height: 65px;
        line-height: 65px;
        text-align: center;
        font-size: $font-size-medium;
        color: $color-theme;
      }

      .item {
        display: flex;
        box-sizing: border-box;
        align-items: center;
        padding: 0 20px 20px 20px;

        .icon {
          flex: 0 0 60px;
          width: 60px;
          padding-right: 20px;
        }

        .text {
          display: flex;
          flex-direction: column;
          justify-content: center;
          flex: 1;
          line-height: 20px;
          overflow: hidden;
          font-size: $font-size-medium;

          .name {
            margin-bottom: 10px;
            color: $color-text;
          }

          .desc {
            color: $color-text-d;
          }
        }
      }
    }

    .loading-container {
      position: absolute;
      width: 100%;
      top: 50%;
      transform: translateY(-50%);
    }
  }
}
</style>
